package com.slz3.business.module.memberManagement.memShoppingCar.steward;

import com.google.gson.Gson;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.module.memberManagement.memShoppingCar.domain.MemShoppingCar;
import com.slz3.business.module.memberManagement.memShoppingCar.enums.MemShoppingCarTypeFlag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @FileName: MemShoppingCarSynr
 * @Description:购物车消息处理
 */

@Component
@Slf4j
public class MemShoppingCarSynr {


    /**
     * 添加购物车同步处理方法
     */
    public static void memShoppingCarAdd(){
        Object shoppingCar = null;
        MemShoppingCar memShoppingCar = null;
        Map<String,Object> map = null;
        try {
            map = (Map<String,Object>)Jedis.db0.leftPop(RK.USER_SHOPPTING_ADDCAR);
            //消费逻辑
            log.info("消费消息:" + map);
            memShoppingCar = new MemShoppingCar();
            memShoppingCar.setSerUserId(UUID.fromString(map.get("userId").toString()));
            memShoppingCar.setSerCount(Integer.parseInt(map.get("count").toString()));
            memShoppingCar.setSerItemId(UUID.fromString(map.get("itemId").toString()));
            memShoppingCar.setSerType("1".equals(map.get("type").toString())?MemShoppingCarTypeFlag.XIANJIN:MemShoppingCarTypeFlag.JIFEN);
            //处理完删除set集合
            Services.memShoppingCar.save(memShoppingCar);
            RedisClient.redisTemplate.opsForSet().pop(RK.USER_SHOPPTING_ADDCAR.concat("Set"));
        }catch (Exception e){
            Set members = Jedis.db0.getSet(RK.USER_SHOPPTING_ADDCAR.concat("Set"));
            System.out.println(members);
            Iterator iterator = members.iterator();
            //遍历set集合
            while(iterator.hasNext()){
                map =(HashMap<String,Object>)iterator.next();
                //消费逻辑
                log.info("消费消息:" + map.toString());
                memShoppingCar = new MemShoppingCar();
                memShoppingCar.setSerUserId(UUID.fromString(map.get("userId").toString()));
                memShoppingCar.setSerCount(Integer.parseInt(map.get("count").toString()));
                memShoppingCar.setSerItemId(UUID.fromString(map.get("itemId").toString()));
                memShoppingCar.setSerType("1".equals(map.get("type").toString())?MemShoppingCarTypeFlag.XIANJIN:MemShoppingCarTypeFlag.JIFEN);
                //处理完删除set集合
                Services.memShoppingCar.save(memShoppingCar);
            }

            //删除set
            RedisClient.redisTemplate.opsForSet().pop(RK.USER_SHOPPTING_ADDCAR.concat("Set"));
        }
    }

    /**
     * 删除购物车同步处理方法
     */
    public static void memShoppingCarDelete(){
        List<String> list = null;
        List<UUID> pkList= new ArrayList<>();
        try{
            list = (List<String>)Jedis.db0.leftPop(RK.SHOPPTING_CAR_DELETE);
            System.out.println(list);
            for(String s:list){
                pkList.add(UUID.fromString(s));
            }
            Services.memShoppingCar.delete(Services.memShoppingCar.findAllByUuids(pkList));
            RedisClient.redisTemplate.opsForSet().pop(RK.SHOPPTING_CAR_DELETE.concat("Set"));
        }catch (Exception e){
            Set members = Jedis.db0.getSet(RK.SHOPPTING_CAR_DELETE.concat("Set"));
            System.out.println(members);
            Iterator iterator = members.iterator();
            //遍历set集合
            while(iterator.hasNext()){
                pkList = new ArrayList<>();
                list = (List<String>) iterator.next();
                for(String s:list){
                    pkList.add(UUID.fromString(s));
                }
                Services.memShoppingCar.delete(Services.memShoppingCar.findAllByUuids(pkList));
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(RK.SHOPPTING_CAR_DELETE.concat("Set"));
        }
    }

    /**
     * 修改购物车同步处理方法
     */
    public static void memShoppingCarModify(){

        MemShoppingCar memShoppingCar = null;
        Map<String,Object> map = null;
        try {
            map = (Map<String,Object>)Jedis.db0.leftPop(RK.SHOPPTING_CAR_MODIFY);
            //消费逻辑
            log.info("消费消息:" + map);
            memShoppingCar = new MemShoppingCar();
            memShoppingCar.setSerCount(Integer.parseInt(map.get("count").toString()));
            memShoppingCar.setUuid(UUID.fromString(map.get("carId").toString()));
            memShoppingCar.setSerUserId(UUID.fromString(map.get("userId").toString()));
            //处理完删除set集合
            Services.memShoppingCar.modify(memShoppingCar);
            RedisClient.redisTemplate.opsForSet().pop(RK.SHOPPTING_CAR_MODIFY.concat("Set"));
        }catch (Exception e) {
            //主数据set 集合
            Set members = Jedis.db0.getSet(RK.SHOPPTING_CAR_MODIFY.concat("Set"));
            System.out.println(members);
            Iterator iterator = members.iterator();
            //遍历set集合
            while (iterator.hasNext()) {
                HashMap<String, Object> nextMap = (HashMap<String, Object>) iterator.next();
                memShoppingCar = new MemShoppingCar();
                memShoppingCar.setSerCount(Integer.parseInt(nextMap.get("count").toString()));
                memShoppingCar.setUuid(UUID.fromString(nextMap.get("carId").toString()));
                memShoppingCar.setSerUserId(UUID.fromString(map.get("userId").toString()));
                //处理完删除set集合
                Services.memShoppingCar.modify(memShoppingCar);
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(RK.SHOPPTING_CAR_MODIFY.concat("Set"));
        }
    }

}